<template>
    <el-main>
      <div>
        <h1>租房分期</h1>
      </div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd(1)"
            v-hasPermi="['financial:product:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['financial:product:edit']"
          >修改</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getStageList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="stagetList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="产品名称" align="center" prop="jrProductName" />
        <el-table-column label="资金提供方" align="center" prop="companyName" />
        <el-table-column label="分期期限(-1表示无限期)" align="center" prop="instalmentPeriod" />
        <el-table-column label="授信额度(单位：%，比如80%，存的是80)" align="center" prop="creditLine" />
        <el-table-column label="还款方式" align="center" prop="repaymentType">
          <template slot-scope="scope">
            <span v-if="scope.row.repaymentType==1">
              {{"按月还款"}}
            </span>
            <span v-if="scope.row.repaymentType==2">
              {{"按季还款"}}
            </span>
            <span v-if="scope.row.repaymentType==3">
              {{"按租约周期支付"}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="银行名称" align="center" prop="bankName" />
        <el-table-column label="状态" align="center" prop="delFlg">
          <template slot-scope="scope">
            <span v-if="scope.row.delFlg==0">
              {{"正常"}}
            </span>
            <span v-if="scope.row.delFlg==1">
              {{"作废"}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row,0)"
              v-hasPermi="['system:product:edit']"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:product:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:product:remove']"
            >作废</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total1>10"
        :total="total1"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getStageList"
      />

      <div>
        <h1>收房贷</h1>
      </div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd(2)"
            v-hasPermi="['financial:product:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['financial:product:edit']"
          >修改</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getCollectList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="collectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="产品名称" align="center" prop="jrProductName" />
        <el-table-column label="资金提供方" align="center" prop="companyName" />
        <el-table-column label="分期期限(-1表示无限期)" align="center" prop="instalmentPeriod" />
        <el-table-column label="授信额度(单位：%，比如80%，存的是80)" align="center" prop="creditLine" />
        <el-table-column label="还款方" align="center" prop="repaymentType">
          <template slot-scope="scope">
            <span v-if="scope.row.repaymentType==1">
              {{"按月还款"}}
            </span>
            <span v-if="scope.row.repaymentType==2">
              {{"按季还款"}}
            </span>
            <span v-if="scope.row.repaymentType==3">
              {{"按租约周期支付"}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="银行名称" align="center" prop="bankName" />
        <el-table-column label="状态" align="center" prop="delFlg">
          <template slot-scope="scope">
            <span v-if="scope.row.delFlg==0">
              {{"正常"}}
            </span>
            <span v-if="scope.row.delFlg==1">
              {{"作废"}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row,0)"
              v-hasPermi="['system:product:edit']"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:product:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:product:remove']"
            >作废</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total2>0"
        :total="total2"
        :page.sync="queryParams2.pageNum"
        :limit.sync="queryParams2.pageSize"
        @pagination="getCollectList"
      />

      <div>
        <h1>应收租约保理</h1>
      </div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd(3)"
            v-hasPermi="['financial:product:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['financial:product:edit']"
          >修改</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getLeaseList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="leaseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="产品名称" align="center" prop="jrProductName" />
        <el-table-column label="资金提供方" align="center" prop="companyName" />
        <el-table-column label="分期期限(-1表示无限期)" align="center" prop="instalmentPeriod" />
        <el-table-column label="授信额度(单位：%，比如80%，存的是80)" align="center" prop="creditLine" />
        <el-table-column label="还款方" align="center" prop="repaymentType">
          <template slot-scope="scope">
            <span v-if="scope.row.repaymentType==1">
              {{"按月还款"}}
            </span>
            <span v-if="scope.row.repaymentType==2">
              {{"按季还款"}}
            </span>
            <span v-if="scope.row.repaymentType==3">
              {{"按租约周期支付"}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="银行名称" align="center" prop="bankName" />
        <el-table-column label="状态" align="center" prop="delFlg">
          <template slot-scope="scope">
            <span v-if="scope.row.delFlg==0">
              {{"正常"}}
            </span>
            <span v-if="scope.row.delFlg==1">
              {{"作废"}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row,0)"
              v-hasPermi="['system:product:edit']"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:product:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:product:remove']"
            >作废</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total3>0"
        :total="total3"
        :page.sync="queryParams3.pageNum"
        :limit.sync="queryParams3.pageSize"
        @pagination="getLeaseList"
      />

      <!-- 添加或修改金融产品管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
        <el-form :disabled="readonly" ref="form" :model="form" :rules="rules" label-width="80px"  label-position="top">
          <el-form-item label="产品名称" prop="jrProductName">
            <el-input v-model="form.jrProductName" placeholder="请输入产品名称" />
          </el-form-item>
          <el-form-item label="资金提供方" prop="companyId">
            <el-select v-model="form.companyId" placeholder="请选择资金提供方">
              <el-option :label="item.name" :value="item.id" v-for="item in companys"/>
            </el-select>
          </el-form-item>
          <el-form-item label="分期期限(-1表示无限)" prop="instalmentPeriod">
            <el-radio v-model="form.instalmentPeriod" label="-1">{{"无限期"}}</el-radio>
            <el-radio v-model="form.instalmentPeriod" label="11">{{"11个月"}}</el-radio>
            <el-radio v-model="form.instalmentPeriod" label="23">{{"23个月"}}</el-radio>
          </el-form-item>
          <el-form-item label="授信额度" prop="creditLine">
            <el-input v-model="form.creditLine" placeholder="请输入授信额度(单位：%，比如80%，存的是80)"/>
          </el-form-item>
          <el-form-item label="还款方式" prop="repaymentType">
            <el-select v-model="form.repaymentType" placeholder="请选择还款方式">
              <el-option :label="item.name" :value="item.id" v-for="item in repayments"/>
            </el-select>
          </el-form-item>
          <!--    当jrProductType=1 flag=true -->
          <el-row v-if="flag">
            <el-form-item label="支付PC链接(URL)">
              <el-input v-model="form.urlPayPc" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label="支付移动链接(URL)">
              <el-input v-model="form.urlPayMoble" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label="补全信息PC(URL)">
              <el-input v-model="form.urlAddDetailInfoPc" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label="补全信息移动(URL)">
              <el-input v-model="form.urlAddDetailInfoMobile" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label="App下载(URL)">
              <el-input v-model="form.urlAppDownload" placeholder="请输入内容" />
            </el-form-item>
          </el-row>

          <el-form-item label="银行账户类型" prop="accountType">
            <el-select v-model="form.accountType" placeholder="请选择银行账户类型" @change="bankType">
              <el-option :label="item.name" :value="item.id" v-for="item in bankTypes"/>
            </el-select>
          </el-form-item>


          <el-row v-if="lock">
            <el-col>
              <el-form-item label="开户行所在省" prop="bankAccountProvince">
                <el-select v-model="form.bankAccountProvince" placeholder="请选择开户行所在省">
                  <el-option :label="item.regionName" :value="item.regionId" v-for="item in provinces"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="银行名称" prop="bankName">
                <el-select v-model="form.bankName" placeholder="请选择开户行所在省">
                  <el-option :label="item.bankName" :value="item.bankName" v-for="item in banks"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="账户姓名" prop="accountName">
                <el-input v-model="form.accountName" placeholder="请输入账户姓名" />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="银行卡号" prop="bankCardNumber">
                <el-input v-model="form.bankCardNumber" placeholder="请输入银行卡号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="type">
            <el-col>
              <el-form-item label="开户行(支行)" prop="openingBank">
                <el-input v-model="form.openingBank" placeholder="请输入开户行(支行)" />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="联行行号" prop="bankBranchCodeLh">
                <el-input v-model="form.bankBranchCodeLh" placeholder="请输入联行行号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="申请条件" prop="applicationConditions">
            <el-input type="textarea" rows="2" v-model="form.applicationConditions" placeholder="请输入申请条件" />
          </el-form-item>
          <el-form-item label="风控要求" prop="riskRequirement">
            <el-input type="textarea" rows="2" v-model="form.riskRequirement" placeholder="请输入风控要求" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

    </el-main>

</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct,getProvinceList,getBankList } from "@/api/financial/product";

export default {
  name: "Product",
  components: {},
  data() {
    return {

      companys: [
        {id: 1, name: "久金所"},
        {id: 2, name: "应花分期"}],
      repayments: [
        {id: 1, name: "按月还款"},
        {id: 2, name: "按季还款"},
        {id: 3, name: "按租约周期支付"},
      ],
      bankTypes: [
        {
          id: 0, name: "请选择"
        },
        {
          id: 1, name: "对私账户"
        }, {
          id: 2, name: "对公账户"
        }],
      type: null,
      lock: null,
      flag: null,
      readonly:false,
      provinces: [],
      //银行
      banks: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total1: 0,
      total2:0,
      total3:0,
      // 【请填写功能名称】表格数据
      stagetList: [],
      collectList: [],
      leaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      //分期
      queryParams1: {
        jrProductType: null,
        pageNum:1,
        pageSize:5,
      },
      //收房贷
      queryParams2: {
        jrProductType: null,
        pageNum:1,
        pageSize:5,
      },
      //保理
      queryParams3: {
        pageNum:1,
        pageSize:5,
        jrProductType: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jrProductName: [
          {required: true, message: "产品名称不能为空", trigger: "blur"}
        ],
        companyName: [
          {required: true, message: "资金提供方不能为空", trigger: "blur"}
        ],
        jrProductType: [
          {required: true, message: "金融产品类型不能为空", trigger: "change"}
        ],
        repaymentType: [
          {required: true, message: "还款方式不能为空", trigger: "change"}
        ],
        delFlg: [
          {required: true, message: "删除标识", trigger: "blur"}
        ],
        createUserid: [
          {required: true, message: "创建用户ID不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateUserid: [
          {required: true, message: "更新用户ID不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },


  created() {
    this.getStageList();
    this.getCollectList();
    this.getLeaseList();
    this.getProvince();
    this.getBankName();
  },


  methods: {
    /** 查询【请填写功能名称】列表 */
    getStageList() {
      this.loading = true;
      this.queryParams1.jrProductType = 1;
      listProduct(this.queryParams1).then(response => {
        this.stagetList = response.rows;
        this.total1= response.total;
        this.loading = false;
      });
    },
    getCollectList() {
      this.loading = true;
      this.queryParams2.jrProductType = 2;
      listProduct(this.queryParams2).then(response => {
        this.collectList = response.rows;
        this.total2= response.total;
        this.loading = false;
      });
    },
    getLeaseList() {
      this.loading = true;
      this.queryParams3.jrProductType = 3;
      listProduct(this.queryParams3).then(response => {
        this.leaseList = response.rows;
        this.total3= response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    //银行卡账户类型
    bankType() {
      if (this.form.accountType == 0) {
        this.lock = false;
        // this.type = false;
      }
      if (this.form.accountType == 1) {
        this.lock = true;
        this.type = false;
      }
      if (this.form.accountType == 2) {
        this.lock = true;
        this.type = true;
      }

    },
    // this.resetForm("form"){};

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(val) {
      this.reset();
      this.open = true;
      this.form.jrProductType = val;
      this.title = "添加【请填写功能名称】";
      if (val == 1) {
        this.flag = true;
      } else {
        this.flag = false;
      }
    },
    getProvince() {
      getProvinceList().then(response => {
        this.provinces = response.data;
      });
    },
    getBankName() {
      getBankList().then(response => {
        this.banks = response.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row,val) {
      this.reset();
      const id = row.id || this.ids
      if(val==0){
        this.readonly=true;
      }else {
        this.readonly=false;
      }
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        if(val==0){
          this.title = "查看";
        }else {
          this.title = "修改";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              // this.msgSuccess("修改成功");
              this.open = false;
              this.getStageList();
              this.getCollectList();
              this.getLeaseList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getStageList();
              this.getCollectList();
              this.getLeaseList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delProduct(ids);
      }).then(() => {
        this.getStageList();
        this.getCollectList();
        this.getLeaseList();
        this.msgSuccess("删除成功");
      })
    },
  }
}

</script>
